using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPC : Entity{
    private int _taskId;
    public void SetTaskId(int taskId)
    {
        _taskId = taskId;
        RefreshTaskState();
    }
    public int GetTaskId()
    {
        return _taskId;
    }
    public SpriteRenderer srTaskState;

    public void SetTaskFinish(bool isFinish)
    {
        var addName = isFinish ? "Sprite_sign1" : "Sprite_sign2";
        XRes.LoadAsset<Sprite>(addName, (sprite) =>
         {
             srTaskState.sprite = sprite;
             srTaskState.color = isFinish ? Color.yellow : Color.red;
             XRes.ReleaseAsset(sprite);
         });
    }

    protected override void OnEnable()
    {
        base.OnEnable();
        Facade.Instance.RegistEvent(GameEvent.Data.ItemChange, OnHandleItemChange);
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        Facade.Instance.UnRegistEvent(GameEvent.Data.ItemChange, OnHandleItemChange);
    }

    private object OnHandleItemChange(object sender)
    {
        RefreshTaskState();
        return null;
    }

    private void RefreshTaskState()
    {
        var taskItem = TaskMgr.Instance.GetTaskItemById(_taskId);
        if (taskItem == null)
            return;

        if (taskItem.IsFinish())
        {
            srTaskState.gameObject.SetActive(false);
            return;
        }

        SetTaskFinish(taskItem.IsCanFinish());
    }
}
